/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import sirtet.gui.GameBoardPanel;
import sirtet.event.BoardActionEvent;
import sirtet.event.BoardEvent;
import sirtet.tetris.TetrisBoard;
import sirtet.tetris.TetrisPiece;

/**
 * The playground panel at the center of the TetrisBoardPanel
 * @author guangwei.zhu
 */
public class TetrisBoardPanel extends GameBoardPanel {

    public TetrisBoardPanel(TetrisBoard board) {
        super(board);

        this.setHorizontalAlignment(SwingConstants.CENTER);
        this.setVerticalAlignment(SwingConstants.BOTTOM);
        this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (board == null || board.getCurrentPiece() == null) {
            return;
        }

        int i, j;
        Point origin = this.getOrigin();
        Point pos;
        Dimension blockSize = this.getBlockSize();
        TetrisPiece piece;

        // Draw falling piece
        piece = (TetrisPiece) board.getCurrentPiece();
        for (i = 0; i < piece.getHeight(); i++) {
            for (j = 0; j < piece.getWidth(); j++) {
                if (piece.getElement(j, i) != piece.getEmptyElement()) {
                    pos = board.getCurrentPosition();
                    pos.translate(j, i);
                    pos.x *= blockSize.width;
                    pos.y *= blockSize.height;
                    pos.translate(origin.x, origin.y);
                    if ((Integer) piece.getElement(j, i) == TetrisPiece.EMPTY) {
                        renderBlock(g, piece.getElement(j, i), pos, blockSize);
                    } else {
                        renderBlock(g, TetrisPiece.FALLING, pos, blockSize);
                    }
                }
            }
        }
    }

    /**
     * Draw a single block at given position.
     * @param g Graphics object on which the block will be drawn
     * @param block the block object
     * @param point the given position at which the block will be drawn
     * @param size the size of the block
     */
    @Override
    protected void renderBlock(Graphics g, Object block, Point point, Dimension size) {
        int element = (Integer) block;
        Color color;

        if (element != TetrisPiece.EMPTY) {
            switch (element) {
                case TetrisPiece.NORMAL:
                    color = Color.DARK_GRAY;
                    break;
                case TetrisPiece.FRAGILE:
                    color = Color.GRAY;
                    break;
                case TetrisPiece.FALLING:
                    color = Color.RED;
                    break;
                default:
                    color = Color.YELLOW;
            }
            g.setColor(color);
            g.fill3DRect(point.x, point.y, size.width, size.height, true);
        } else {
            g.setColor(this.getBackground());
            g.fillRect(point.x, point.y, size.width, size.height);
        }
    }

    @Override
    public void boardActionTriggered(BoardActionEvent evt) {
        this.repaint();
    }

    @Override
    public void boardChanged(BoardEvent evt) {
        this.repaint();
    }

    @Override
    public void boardDisposing(BoardEvent evt) {
        this.board = null;
    }
}
